package assetservicelogic

import (
	"context"
	"github.com/zeromicro/go-zero/core/logx"
	"property_service/dao/model"
	"property_service/dao/query"
	"property_service/internal/svc"
	"property_service/property_service"
	"property_service/utils/BeanUtils"
	"property_service/utils/result"
)

type AssetPurchaseRequestEditLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewAssetPurchaseRequestEditLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AssetPurchaseRequestEditLogic {
	return &AssetPurchaseRequestEditLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// AssetPurchaseRequestEdit 更新采购申请单表
func (l *AssetPurchaseRequestEditLogic) AssetPurchaseRequestEdit(in *property_service.AssetPurchaseRequest) (*property_service.Response, error) {
	// 获取采购申请单表模型的查询构造器
	m := query.Use(l.svcCtx.Gorm).AssetPurchaseRequest
	// 构建查询条件：根据ID查询
	do := m.WithContext(l.ctx).Where(m.ID.Eq(in.ID))
	// 执行更新操作，仅更新指定字段，不包含ID、TenantID、StockID、UpdatedAt、CreatedAt
	if _, err := do.UpdateColumns(&model.AssetPurchaseRequest{
		RequestCode:    in.RequestCode,                            // 申请单号
		PurchaseMonth:  BeanUtils.GetDateFormat(in.PurchaseMonth), // 购置年月
		RequestType:    in.RequestType,                            // 申请类型
		RequestTypeID:  in.RequestTypeID,                          //申请类型id
		PlanNumber:     in.PlanNumber,                             // 计划号
		RequestDept:    in.RequestDept,                            // 申请科室
		RequestDeptID:  in.RequestDeptID,                          //申请科室id
		ApplicantID:    in.ApplicantID,                            // 申请人id
		Applicant:      in.Applicant,                              // 申请人
		CompileDate:    BeanUtils.GetDateFormat(in.CompileDate),   // 编制日期
		Remarks:        in.Remarks,                                // 备注
		PurchaseStatus: in.PurchaseStatus,                         // 采购状态（0-申请 1-成功）
		StatusID:       in.StatusID,                               // 状态ID(关联状态表StatusDisposition)
		StatusName:     in.StatusName,                             // 采购状态（0-申请 1-成功）
		UpdatedAtuser:  in.UpdatedAtuser,                          // 创建用户
	}); err != nil {
		// 如果更新失败，返回错误响应
		return result.Err(err).Ed()
	}
	// 更新成功，返回成功响应
	return result.Suc().EditEd()
}
